<?php

namespace AdminModule;

use Nette\Application\UI,
    Nette\Security as NS;

class SignPresenter extends BasePresenter {



    public function renderIn() {
	if ($this->getUser()->isLoggedIn())
	    $this->redirect('out');
    }

    /**
     * Sign in form component factory.
     * @return Nette\Application\UI\Form
     */
    protected function createComponentSignInForm() {
	$form = new UI\Form;

	$form->addGroup('Jazyk - Language');
	$form->addSelect('language', 'Jazyk:', array('CZ' => 'Čeština', 'EN' => 'English'));

	$form->addGroup('Přihlášení');
	$form->addText('username', 'Jméno:')
		->setRequired('Please provide a username.');
	$form->addPassword('password', 'Heslo:')
		->setRequired('Please provide a password.');
	$form->addSubmit('send', 'Přihlásit');
	$form->onSuccess[] = callback($this, 'signInFormSubmitted');

	return $form;
    }

    public function signInFormSubmitted($form) {
	try {
	    $values = $form->getValues();
	    $this->getUser()->login($values->username, $values->password);
	    $this->redirect('PatientCard:find');
	} catch (NS\AuthenticationException $e) {
	    $form->addError($e->getMessage());
	}
    }

    public function actionOut() {
	$this->getUser()->logout();
	$this->flashMessage('You have been signed out.');
	$this->redirect('in');
    }

}
